import machine
from machine import ADC, Pin,I2C, deepsleep
from machine import RTC
import utime
import esp32
import PFC8563
import IIC256
adc = ADC(Pin(34))
adc.atten(ADC.ATTN_11DB)    # set 11dB input attenuation (voltage range roughly 0.0v - 3.6v)
adc.width(ADC.WIDTH_12BIT)  
SHT20_address       =   64
T_measurement       =   bytes([0b11110011])
RH_measurement      =   bytes([0b11110101])
Write_user_register =   bytes([0b11100110])
Read_user_register  =   bytes([0b11100111])
Soft_reset          =   bytes([0b11111110])
print(machine.wake_reason())
wake1 = Pin(27, mode = Pin.IN,pull=machine.Pin.PULL_UP)
#level parameter can be: esp32.WAKEUP_ANY_HIGH or esp32.WAKEUP_ALL_LOW
esp32.wake_on_ext0(pin = wake1, level = esp32.WAKEUP_ALL_LOW)
rtc = RTC()
xj=[
b'\x40\x40\x42\x42\x42\x42\x42\xFE\x42\x42\x42\x42\x42\x40\x40\x00\x80\x80\x40\x20\x10\x0C\x03\x00\x03\x0C\x10\x20\x40\x80\x80\x00',#"天",0
b'\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',#"一",1
b'\x00\x00\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x00\x00\x00\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x00',#"二",2
b'\x00\x04\x84\x84\x84\x84\x84\x84\x84\x84\x84\x84\x84\x04\x00\x00\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x00',#"三",3
b'\x00\xFC\x04\x04\x04\xFC\x04\x04\x04\xFC\x04\x04\x04\xFC\x00\x00\x00\x7F\x28\x24\x23\x20\x20\x20\x20\x21\x22\x22\x22\x7F\x00\x00',#"四",4
b'\x00\x02\x42\x42\x42\xC2\x7E\x42\x42\x42\x42\xC2\x02\x02\x00\x00\x40\x40\x40\x40\x78\x47\x40\x40\x40\x40\x40\x7F\x40\x40\x40\x00',#"五",5
b'\x20\x20\x20\x20\x20\x20\x21\x22\x2C\x20\x20\x20\x20\x20\x20\x00\x00\x40\x20\x10\x0C\x03\x00\x00\x00\x01\x02\x04\x18\x60\x00\x00',#"六",6
b'\x00\x00\x00\xBE\x2A\x2A\x2A\xEA\x2A\x2A\x2A\x3E\x00\x00\x00\x00\x00\x44\x42\x49\x49\x49\x49\x7F\x49\x49\x49\x49\x41\x40\x00\x00',#"星",7
b'\x00\x04\xFF\x24\x24\x24\xFF\x04\x00\xFE\x22\x22\x22\xFE\x00\x00\x88\x48\x2F\x09\x09\x19\xAF\x48\x30\x0F\x02\x42\x82\x7F\x00\x00',#"期",8
]

d2=machine.Pin(2,machine.Pin.OUT)
d2.off()
leds=machine.Pin(17,machine.Pin.OUT)
leds.on()
i2c = I2C(1, scl=Pin(18), sda=Pin(19), freq=400000)
def showFast():
    leds.off()
    showSlow()
    for i in range(60):
        da=i2c.readfrom_mem(81,2,3)
        s=((da[0]>>4) * 10) + (da[0]&0x0f)
        m=((da[1]>>4) * 10) + (da[1]&0x0f)
        h=((da[2]>>4) * 10) + (da[2]&0x0f)
        if(s==0): showSlow()
        strs=("%02d"%(s))
        IIC256.lprint(strs,28,1)
        t=rtc.datetime()
        utime.sleep_us(1000000-t[7])
        print(i)
        gett()

        
    IIC256.lprint("       ",27,1)
    leds.on()
    machine.deepsleep((60-s)*1000)

def showSlow():
    da=i2c.readfrom_mem(81,2,8)
    s=((da[0]>>4) * 10) + (da[0]&0x0f)
    m1=da[1]>>4
    m2=da[1]&0x0f
    h1=da[2]>>4
    h2=da[2]&0x0f
    t1=[h1,h2,10,m1,m2]
    d=((da[3]>>4) * 10) + (da[3]&0x0f)
    w=((da[4]>>4) * 10) + (da[4]&0x0f)
    mo=((da[5]>>4) * 10) + (da[5]&0x0f)
    y=((da[6]>>4) * 10) + (da[6]&0x0f)
    IIC256.lprint("20%02d-%02d-%02d"%(y,mo,d),28,2)
    IIC256.lcdAddress(170,4,16,16)
    IIC256.LDS(xj[7])
    IIC256.lcdAddress(170+16,4,16,16)
    IIC256.LDS(xj[8])
    IIC256.lcdAddress(170+32,4,16,16)
    IIC256.LDS(xj[w])
    gett()
    n=0
    for t in t1:
        IIC256.lcdAddress(n*32,0,32,64)
        for i in range(16):
            IIC256.LDS(IIC256.bigzf[t*16+i])
        n=n+1
def gett():
    i2c.writeto(SHT20_address,bytearray(T_measurement))
    utime.sleep_ms(100)
    buf=i2c.readfrom(SHT20_address,3)
    t0=buf[0]*256+buf[1]
    i2c.writeto(SHT20_address,bytearray(RH_measurement))
    utime.sleep_ms(80)
    buf=i2c.readfrom(SHT20_address,3)
    t=buf[0]*256+buf[1]
    sht20rt=(t0/65536*175.72)-46.85
    sht20rh=(t/65536*125)-6
    IIC256.print32("%.2fC %.2f%%   "%(sht20rt,sht20rh),0,8)
    v=adc.read()
    print(v)
    IIC256.print32("%.2fV   "%(v*7.0414/4096),0,12)
def gettime():
        da=i2c.readfrom_mem(81,2,8)
        s=((da[0]>>4) * 10) + (da[0]&0x0f)
        m=((da[1]>>4) * 10) + (da[1]&0x0f)
        h=((da[2]>>4) * 10) + (da[2]&0x0f)
        d=((da[3]>>4) * 10) + (da[3]&0x0f)
        w=((da[4]>>4) * 10) + (da[4]&0x0f)
        mo=((da[5]>>4) * 10) + (da[5]&0x0f)
        y=((da[6]>>4) * 10) + (da[6]&0x0f)
        return (s,m,h,d,w,mo,y)

if machine.wake_reason() == 0:
    IIC256.LCDInit()
    print("poweron")
    PFC8563.sync()
    showFast()
    while 1:
        t=gettime()
        print("%02d:%02d:%02d  20%02d-%02d-%02d  %d"%(t[2],t[1],t[0],t[6],t[5],t[3],t[4]))
        utime.sleep(1)

if machine.wake_reason() == 2:
    print("key")
    showFast()
if machine.wake_reason() == 4:
    showSlow()
    da=i2c.readfrom_mem(81,2,8)
    s=((da[0]>>4) * 10) + (da[0]&0x0f)
    print("timer")
    machine.deepsleep((60-s)*1000)
    



